package hczc.longcai.com.hccrowdfunding.home.second_invest.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.second_invest.activities.AffirmReturnActivity;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.AffirmReturn;
import hczc.longcai.com.hccrowdfunding.home.second_invest.beans.details_items.ConsigneeInfo;


/**
 * 回报确认适配器
 * Created by Administrator on 2016/1/15.
 */
public class AffirmReturnAdapter extends BaseAdapter implements View.OnClickListener {

    private static final String TAG = "AffirmReturnAdapter";
    private Context context;
    private List<AffirmReturn> list;
    private CallBack callBack;

    public AffirmReturnAdapter(Context context, List<AffirmReturn> affirmReturns, CallBack callBack) {
        this.context = context;
        this.list = affirmReturns;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 7;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHoler holer = null;
        int type = getItemViewType(i);
        if (view == null) {
            holer = new ViewHoler();
            switch (type) {
                case 1:
                    view = LayoutInflater.from(context).inflate(R.layout.item_affirm_title, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    view.setTag(holer);
                    break;
                case 2:
                    view = LayoutInflater.from(context).inflate(R.layout.item_affirm_normal, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holer.tab = ((TextView) view.findViewById(R.id.affirm_listview_normal_tab));
                    holer.price = ((TextView) view.findViewById(R.id.affirm_listview_normal_price));
                    view.setTag(holer);
                    break;
                case 3:
                    view = LayoutInflater.from(context).inflate(R.layout.item_affirm_content, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holer.tab = ((TextView) view.findViewById(R.id.affirm_listview_content_tab));
                    holer.content = ((TextView) view.findViewById(R.id.affirm_listview_content_content));
                    view.setTag(holer);
                    break;
                case 4:
                    view = LayoutInflater.from(context).inflate(R.layout.item_affirm_remark, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    view.setTag(holer);

                    break;

                case 5:
                    view = LayoutInflater.from(context).inflate(R.layout.item_affirm_consignee, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holer.tab = ((TextView) view.findViewById(R.id.affirm_listview_consignee_tab));
                    holer.name = ((TextView) view.findViewById(R.id.affirm_listview_consignee_name));
                    holer.phone = ((TextView) view.findViewById(R.id.affirm_listview_consignee_phone));
                    holer.content = ((TextView) view.findViewById(R.id.affirm_listview_consignee_address));
                    holer.consignee = ((RelativeLayout) view.findViewById(R.id.affirm_listview_consignee));
                    view.setTag(holer);
                    break;

                case 6:
                    view = LayoutInflater.from(context).inflate(R.layout.item_affirm_button, null);
                    BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));
                    holer.button = ((Button) view.findViewById(R.id.affirm_listview_consignee_qzf));
                    view.setTag(holer);
                    break;

            }
        }

        holer = ((ViewHoler) view.getTag());
        AffirmReturn affirmReturn = list.get(i);
        switch (type) {
            case 1:

                break;
            case 2:
                if (affirmReturn.getPrice() != null) {
                    holer.tab.setText("应付金额：");
                    holer.price.setText("999元");
                }
                if (affirmReturn.getPracticalPrice() != null) {
                    holer.tab.setText("支持金额：");
                    holer.price.setText(affirmReturn.getPracticalPrice());
                }
                if (affirmReturn.getFreight() != null) {
                    holer.tab.setText("配送费用：");
                    holer.price.setText("免运费");
                }

                break;
            case 3:
                String front = "\u3000\u3000\u3000\u3000\u3000";
                holer.content.setText(front + affirmReturn.getReturnContent());
                break;

            case 5:
                if (affirmReturn.getRiskTip() != null) {
                    holer.name.setVisibility(View.GONE);
                    holer.phone.setVisibility(View.GONE);
                    holer.tab.setText("风险提示");
                    holer.content.setText(affirmReturn.getRiskTip());
                } else {
                    ConsigneeInfo info = affirmReturn.getInfo();
                    Log.d(TAG, "****************************" + info);

                    if (info != null) {
                        String name = info.getName();
                        String phone = info.getPhone();
                        String address = info.getAddress();
                        holer.name.setText(name);
                        holer.name.setVisibility(View.VISIBLE);
                        holer.phone.setText(phone);
                        holer.phone.setVisibility(View.VISIBLE);
                        holer.content.setText(address);
                    }
                    holer.consignee.setOnClickListener(this);
                }

                break;

            case 6:
                holer.button.setOnClickListener(this);

                break;
        }

        return view;
    }

    public interface CallBack {
        void click(View view);
    }

    @Override
    public void onClick(View view) {
        callBack.click(view);
    }

    class ViewHoler {

        TextView tab, price, content, name, phone, address;
        RelativeLayout consignee;
        Button button;

    }
}
